﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services;
using System.Reflection;
using System.IO;
using ADONETDataServicesExtension.Utils;

namespace ADONETDataServicesExtension
{
    public class HttpContextServiceHost : FrameworkEncapsulatedType<IDataServiceHost>, IDataServiceHost
    {
        internal const string FrameworkTypeName = "System.Data.Services.HttpContextServiceHost";

        public HttpContextServiceHost(IDataServiceHost httpContextServiceHost)
            : base(httpContextServiceHost)
        {
        }
        public HttpContextServiceHost(Stream messageBody)
        {
            FrameworkObject = ReflectionHelper.CreateInstance(FrameworkTypeName, messageBody) as IDataServiceHost;
        }

        public Uri AbsoluteRequestUri
        {
            get { return FrameworkObject.AbsoluteRequestUri; }
        }

        public Uri AbsoluteServiceUri
        {
            get { return FrameworkObject.AbsoluteServiceUri; }
        }

        public string GetQueryStringItem(string item)
        {
            return FrameworkObject.GetQueryStringItem(item);
        }

        public void ProcessException(HandleExceptionArgs args)
        {
            FrameworkObject.ProcessException(args);
        }

        public string RequestAccept
        {
            get { return FrameworkObject.RequestAccept; }
        }

        public string RequestAcceptCharSet
        {
            get { return FrameworkObject.RequestAcceptCharSet; }
        }

        public string RequestContentType
        {
            get { return FrameworkObject.RequestContentType; }
        }

        public string RequestHttpMethod
        {
            get { return FrameworkObject.RequestHttpMethod; }
        }

        public string RequestIfMatch
        {
            get { return FrameworkObject.RequestIfMatch; }
        }

        public string RequestIfNoneMatch
        {
            get { return FrameworkObject.RequestIfNoneMatch; }
        }

        public string RequestMaxVersion
        {
            get { return FrameworkObject.RequestMaxVersion; }
        }

        public Stream RequestStream
        {
            get { return FrameworkObject.RequestStream; }
        }

        public string RequestVersion
        {
            get { return FrameworkObject.RequestVersion; }
        }

        public string ResponseCacheControl
        {
            get
            {
                return FrameworkObject.ResponseCacheControl;
            }
            set
            {
                FrameworkObject.ResponseCacheControl = value;
            }
        }

        public string ResponseContentType
        {
            get
            {
                return FrameworkObject.ResponseContentType;
            }
            set
            {
                FrameworkObject.ResponseContentType = value;
            }
        }

        public string ResponseETag
        {
            get
            {
                return FrameworkObject.ResponseETag;
            }
            set
            {
                FrameworkObject.ResponseETag = value;
            }
        }

        public string ResponseLocation
        {
            get
            {
                return FrameworkObject.ResponseLocation;
            }
            set
            {
                FrameworkObject.ResponseLocation = value;
            }
        }

        public int ResponseStatusCode
        {
            get
            {
                return FrameworkObject.ResponseStatusCode;
            }
            set
            {
                FrameworkObject.ResponseStatusCode = value;
            }
        }

        public Stream ResponseStream
        {
            get { return FrameworkObject.ResponseStream; }
        }

        public string ResponseVersion
        {
            get
            {
                return FrameworkObject.ResponseVersion;
            }
            set
            {
                FrameworkObject.ResponseVersion = value;
            }
        }

        internal void VerifyQueryParameters()
        {
            FrameworkObject.GetType().GetMethod("VerifyQueryParameters", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(FrameworkObject, null);
        }
    }
}
